<?php
/**
 * Created by PhpStorm.
 * User: bxm
 * Date: 2022/9/29
 * Time: 12:19
 */

namespace app\common\model;


use think\Request;

class MenuModel extends BaseModel
{
    protected $table = 'menu';

    /**
     * 根据角色获取菜单
     * @param $role_id 角色id
     * @return array|\PDOStatement|string|\think\Collection
     *                 licheng 2020/9/25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMenuLeft($roleArr)
    {
        $s = new RoleMenuModel();
        $list = $s->alias('srm')
            ->leftJoin('menu sm', 'sm.id = srm.menu_id')
            ->where('sm.status', 1)
            ->where('sm.level', 1)
            ->where('sm.parent_id', 0)
            ->where('srm.role_id', 'in', $roleArr)
            ->field('sm.name as title,srm.menu_id,sm.icon,sm.link,sm.path,sm.level,sm.hidden,sm.href')
            ->order('sm.order DESC')
            ->select();
        if ($list) {
            $list = $list->toArray();
        }
        # 合并相同菜单
        $new = [];
        foreach ($list as $key => &$v) {

            $v['meta']['title'] = $v['title'];
            $v['meta']['icon'] = $v['icon'];
            $v['hidden'] = $v['hidden'] ? true : false;
            $v['redirect'] = "noRedirect";
            $v['component'] = "Layout";
            $v['alwaysShow'] = true;
            array_push($new, $v);
        }
        $new = array_values(array_column($new, null, 'menu_id'));
        #查询二级菜单
        $rt = [];
        if ($new) {
            foreach ($new as $keys => &$val) {
                $erList = $s->alias('srm')
                    ->leftJoin('menu sm', 'sm.id = srm.menu_id')
                    ->where('sm.status', 1)
                    ->where('sm.level', 2)
                    ->where('srm.role_id', 'in', $roleArr)
                    ->where('sm.parent_id', $val['menu_id']);

                $erList = $erList->field('sm.name as title,srm.menu_id,sm.icon,sm.link,sm.path,sm.level,sm.hidden,sm.href,sm.note')
                    ->order('sm.order DESC')
                    //->group('sm.link')
                    ->select();
                if (!$erList->isEmpty()) {
                    $erList = $erList->toArray();
                    foreach ($erList as $key => &$val2) {

                        $val2['meta']['title'] = $val2['title'];
                        $val2['meta']['note'] = $val2['note'];
                        $val2['meta']['icon'] = $val2['icon'];
                        $val2['component'] = $val2['link'];
                        $v['hidden'] = $v['hidden'] ? true : false;
                        $sanList = $s->alias('srm')
                            ->leftJoin('menu sm', 'sm.id = srm.menu_id')
                            ->where('sm.status', 1)
                            ->where('sm.level', 3)
                            ->where('srm.role_id', 'in', $roleArr)
                            ->where('sm.parent_id', $val2['menu_id']);

                        $sanList = $sanList->field('sm.name as title,srm.menu_id,sm.icon,sm.link,sm.path,sm.level,sm.hidden,sm.href,sm.note,sm.perms');

                        $sanList = $sanList->order('sm.order DESC')->select();
                        $val2['meta']['child'] = $sanList;
                    }
                    $val['children'] = $erList;
                    array_push($rt, $val);
                }
            }
            return $rt;
        }
        return [];
    }

    /**
     * 在分配页面权限获取 全部菜单
     * @param $role_id
     * @return array
     */
    public function getMenu($role_id)
    {
        $roleArr = explode(',', $role_id);
        $s = new RoleMenuModel();
        $list = $s->alias('srm')
            ->leftJoin('menu sm', 'sm.id = srm.menu_id')
            ->where('sm.status', 1)
            ->where('srm.role_id', 'in', $roleArr)
            ->field('sm.name as title,srm.menu_id,sm.icon,sm.link,sm.path,sm.level,sm.hidden,sm.href')
            ->select();

        if ($list) {
            $list = $list->toArray();
        }

        # 合并相同菜单
        $new = [];
        foreach ($list as $key => &$v) {
            $v['meta']['title'] = $v['title'];
            $v['meta']['icon'] = $v['icon'];
            $v['hidden'] = $v['hidden'] ? true : false;
            $v['redirect'] = "noRedirect";
            $v['component'] = "Layout";
            $v['alwaysShow'] = true;
            array_push($new, $v);
        }
        $new = array_values(array_column($new, null, 'menu_id'));
        return $new;
    }
}